"""
@version:python3
@author:ll
@file:04-QWidget-事件案例2.py
@time:2022/2/10 17:08
"""
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.move_flag = False
        self.setWindowTitle("")
        self.resize(500, 500)
        self.setup_ui()


    def setup_ui(self):  # 添加子控件以及子控件的配置操作
        pass

    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button() == Qt.LeftButton:
            self.move_flag = True
            # 确定两个点：鼠标按下时的点、窗口当前所在的原始点
            self.mouse_x = QMouseEvent.globalX()
            self.mouse_y = QMouseEvent.globalY()
            print(self.mouse_x,self.mouse_y)

            self.origin_x = self.x()
            self.origin_y = self.y()


    def mouseMoveEvent(self, QMouseEvent):
        if self.move_flag:
            # 计算移动向量
            move_x = QMouseEvent.globalX() - self.mouse_x
            move_y = QMouseEvent.globalY() - self.mouse_y
            print(move_x,move_y)
            dst_x = self.origin_x + move_x
            dst_y = self.origin_y + move_y
            self.move(dst_x,dst_y)
    def mouseReleaseEvent(self, QMouseEvent):
        self.move_flag = False


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    window.setMouseTracking(True)

    sys.exit(app.exec_())
